<?php

namespace App\Repositories;


use App\Interfaces\UserCollectionService;
use App\Models\UserCollection;
use Illuminate\Contracts\Validation\ValidationException;

class UserCollectionRepository implements UserCollectionService{


    public function add($user_id,$type,$id){

        //是否被收藏过
        if ($this->isInCollection($user_id,$type,$id)){

            return false;
        }

        $userCollection = UserCollection::create(['type'=>$type,'user_id'=>$user_id,'reference_id'=>$id,'add_time'=>date('Y-m-d H:i:s',time())]);

        return $userCollection;

    }

    public function isInCollection($user_id,$type,$id){

        $userCollection = UserCollection::where('user_id',$user_id)
            ->where('reference_id',$id)
            ->where('type',$type)->first();


        if ($userCollection) return true;
    }


    public function lists($user_id,$type,$page,$pageSize=12){

        return UserCollection::orderBy('add_time','desc')
            ->where('user_id',$user_id)
            ->where('type',$type)->paginate($pageSize);

    }

    public function cancel($user_id,$type,$id){


        $cancel = UserCollection::where('user_id',$user_id)
            ->where('reference_id',$id)
            ->where('type',$type)->delete();

        return $cancel;

    }

}